﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obra.Api;

namespace Obra.SmugMug.Api
{
    public partial class Album
    {
        private readonly AlbumContract album;
        private readonly ISession session;
        private readonly ISmugMugClient client;

        /// <summary>
        /// c-tor
        /// </summary>
        /// <param name="album">
        /// AlbumContract returned by SmugMug, which this class proxies.
        /// </param>
        /// <param name="session">
        /// The current sesssion identifier with SmugMug.
        /// </param>
        /// <param name="client">
        /// Client used to access SmugMug.
        /// </param>
        public Album(
            AlbumContract album,
            ISession session,
            ISmugMugClient client)
        {
            this.album = album;
            this.session = session;
            this.client = client;
        }

        /// <summary>
        /// This class serves as a proxy for the underlying REST response from
        /// smugmug.com.
        /// </summary>
        internal AlbumContract AlbumContract
        {
            get { return album; }
        }

        /// <summary>
        /// WCF interface for interacting with smugmug.com.
        /// </summary>
        public ISmugMugClient SmugMug
        {
            get { return this.client; }
        }

        public SortDirection SortDirection
        {
            get { return this.AlbumContract.SortDirection; }
            set { this.SmugMug.ChangeAlbumSortDirection(this.session.SessionId, this.AlbumContract.Id, (value == SortDirection.Ascending)); }
        }

        public SortMethod SortMethod
        {
            get { return this.AlbumContract.SortMethod; }
            set { this.SmugMug.ChangeAlbumSortMethod(this.session.SessionId, this.AlbumContract.Id, value.ToString() ); }
        }

#if EVERY_MODE_OPTION_EXPOSED
        private string GetModeString()
        {
            string canRankBit = this.CanRank ? "r" : "-";
            string cleanBit = this.Clean ? "c" : "-";
            string commentsBit = this.Comments ? "C" : "-";
            string defaultColorBit = this.DefaultColor ? "d" : "-";
            string exifBit = this.Exif ? "e" : "-";
            string externalBit = this.External ? "E" : "-";
            string familyEditBit = this.FamilyEdit ? "f" : "-";
            string fileNamesBit = this.FileNames ? "n" : "-";
            string friendEditBit = this.FriendEdit ? "F" : "-";
            string geographyBit = this.Geography ? "g" : "-";
            string headerBit = this.Header ? "H" : "-";
            string hideOwnerBit = this.HideOwner ? "h" : "-";
            string largesBit = this.Larges ? "l" : "-";
            string originalsBit = this.Originals ? "o" : "-";
            string protectedBit = this.Protected ? "p" : "-";
            string printableBit = this.Printable ? "P" : "-";
            string publicBit = this.Public ? "u" : "-";
            string shareBit = this.Share ? "S" : "-";
            string smugSearchableBit = this.SmugSearchable ? "s" : "-";
            string watermarkingBit = this.Watermarking ? "W" : "-";
            string worldSearchableBit = this.WorldSearchable ? "w" : "-";
            string xlargesBit = this.XLarges ? "x" : "-";
            string x2largesBit = this.X2Larges ? "2" : "-";
            string x3largesBit = this.X3Larges ? "3" : "-";

            return canRankBit +
                cleanBit +
                commentsBit +
                defaultColorBit +
                exifBit +
                externalBit +
                familyEditBit +
                fileNamesBit +
                friendEditBit +
                geographyBit +
                headerBit +
                hideOwnerBit +
                largesBit +
                originalsBit +
                protectedBit +
                printableBit +
                shareBit +
                smugSearchableBit +
                watermarkingBit +
                worldSearchableBit +
                xlargesBit +
                x2largesBit +
                x3largesBit;
        }
#endif

        /// <summary>
        /// An album mode string similar to a directory listing mode string.  A 
        /// condensed representation of a Album's boolean flags.
        /// </summary>
        public string Mode
        {
            get { return this.GetModeString(); }
        }
        
        /// <summary>
        /// Form an album mode string.
        /// </summary>
        /// <remarks>
        /// <para>
        /// I am not sure which properties are important, so this is my first
        /// pass selection.
        /// </para>
        /// </remarks>
        private string GetModeString()
        {
            // -- essentials
            string geographyBit = this.Geography ? "g" : "-";

            string essentials = String.Format("{0}",
                geographyBit);

            // -- look & feel
            //string cleanBit = this.Clean ? "l" : "-";
            //string exifBit = this.Exif ? "E" : "-";
            //string fileNamesBit = this.FileNames ? "n" : "-";
            //string headerBit = this.Header ? "H" : "-";

            string lookAndFeel = "";

            // -- security & privacy
            string externalBit = this.External ? "e" : "-";
            //string hideOwnerBit = this.HideOwner ? "h" : "-";
            string protectedBit = this.Protected ? "p" : "-";
            string publicBit = this.Public ? "P" : "-";
            string smugSearchableBit = this.SmugSearchable ? "S" : "-";
            //string watermarkingBit = this.Watermarking ? "w" : "-";
            string worldSearchableBit = this.WorldSearchable ? "s" : "-";
            //string largesBit = this.Larges ? "L" : "-";
            string originalsBit = this.Originals ? "O" : "-";
            //string xlargesBit = this.XLarges ? "X" : "-";
            //string x2largesBit = this.X2Larges ? "2" : "-";
            //string x3largesBit = this.X3Larges ? "3" : "-";

            string securityAndPrivacy = String.Format("{0}{1}{2}{3}{4}{5}",
                externalBit,
                protectedBit,
                publicBit,
                smugSearchableBit,
                worldSearchableBit,
                originalsBit);

            // -- social
            string canRankBit = this.CanRank ? "c" : "-";
            string commentsBit = this.Comments ? "C" : "-";
            string familyEditBit = this.FamilyEdit ? "F" : "-";
            string friendEditBit = this.FriendEdit ? "f" : "-";
            string shareBit = this.Share ? "s" : "-";

            string social = String.Format("{0}{1}{2}{3}{4}",
                canRankBit,
                commentsBit,
                familyEditBit,
                friendEditBit,
                shareBit);

            // printing and sales
            //string printableBit = this.Printable ? "P" : "-";

            return String.Format("{0}{1}{2}{3}",
                essentials,
                lookAndFeel,
                securityAndPrivacy,
                social);
        }
    }
}
